import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Student{
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

class Imp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1,Integer o2){
        return o2.compareTo(o1);
    }
}

public class test {
    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int array[] = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);

        // 向下调整建堆:O(N)
        testHeap.createHeap();
        System.out.println("======");
        // O(N * logN)
        testHeap.heapSort();

        System.out.println("======");
    }
    public static void main5(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Imp());
        // 建立k个元素的大根堆
        int[] array = {11,12,9,10,1,2};
        int k = 3;

        // K * logK
        for(int i = 0;i < k;i++){
            priorityQueue.offer(array[i]);
        }

        // O((N-k) * logK)
        for(int i = k;i < array.length;i++){
            int top = priorityQueue.peek();
            if(top > array[i]){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        // 总的时间复杂度: O(N * logK)

        // K * logK
        int[] ret = new int[k];
        for(int i = 0;i < k;i++){
            ret[i] = priorityQueue.poll();
        }

        // return ret;
    }

    public static void main4(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        int[] arr = {19,16,2,3,10,5};

        // 插入元素向上调整,O(N*logN)
        for(int i = 0;i < arr.length;i++){
            priorityQueue.offer(arr[i]);
        }

        int k = 3;
        int[] ret = new int[k];
        // O(k*logN)
        // 向下调整 k 次
        for(int i = 0;i < k;i++){
            ret[i] = priorityQueue.poll();
        }

        System.out.println(Arrays.toString(ret));
    }
    public static void main3(String[] args) {
        Imp imp = new Imp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(imp);
        priorityQueue.offer(1);
        priorityQueue.offer(5);
        priorityQueue.offer(6);

        System.out.println(priorityQueue.poll());// 1
        System.out.println(priorityQueue.poll());// 5


    }
    public static void main2(String[] args) {
        // 默认是小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(1);
        priorityQueue.offer(5);
        priorityQueue.offer(6);

        System.out.println(priorityQueue.poll());// 1
        System.out.println(priorityQueue.poll());// 5

        PriorityQueue<Student> priorityQueue1 = new PriorityQueue<>();
        // 增加一个对象元素
        priorityQueue1.offer(new Student(10,"张三"));
        priorityQueue1.offer(new Student(15,"李四"));
        // 不能直接比较,需要指定比较关系


    }
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int array[] = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);

        testHeap.createHeap();
        System.out.println("======");

        // testHeap.push(80);
        System.out.println("======");

        System.out.println(testHeap.pop());
        System.out.println("======");
    }
}
